# coding: utf8

import xlrd
import xlwt


def read_excel_by_xlrd(filename, sheetname):
    """
    注意：xlrd的2.0以上版本不支持后缀名.xlsx文件的读取，仅支持.xls文件的读取。
    需要读取xlsx文件时，需要降低版本，如：1.2.0。
    """
    data = xlrd.open_workbook(filename)
    sheet1 = data.sheet_by_name(sheetname)
    for rowi in range(sheet1.nrows):
        row_list = sheet1.row_values(rowi)
        # if len(row_list[0]) > 1:
        print(row_list)


def write_excel_by_xlwt(filename, sheet='mysheet'):
    wk = xlwt.Workbook()
    mysheet = wk.add_sheet(sheet, cell_overwrite_ok=True)
    data = [["李明", 15, "游泳"], ["张力", 25, "登山"]]
    for index, row in enumerate(data):
        for col in range(3): 
            mysheet.write(index, col, label=row[col])
    wk.save(filename)


if __name__ == "__main__":
    print("read excel file: stu54a.xlsx--Sheet1")
    read_excel_by_xlrd("student551.xlsx", "Sheet1")
    print("write excel file: stu54b.xlsx--mysheet")
    write_excel_by_xlwt("stu54b.xlsx", "mysheet")
    print("read excel file: stu54b.xlsx--mysheet")
    read_excel_by_xlrd("stu54b.xlsx", "mysheet")
